//桌面容器里的方法
import {
	Toast
} from 'vant';
import store from '@/store/index';
window.xhContainer = {};
//发送消息
export const postMessage = (params) => {
	window.xhContainer[params.name] = params;
	let data = JSON.stringify({
		'Command': params.name,
		'Data': params.data
	})
	console.log('\x1B[40m%s\x1B[0m', '------', `container发送[${params.name}]:`, data)
	if (window.chrome.webview) {
		window.chrome.webview.postMessage(data);
	} else {
		console.log('请在windows容器环境下运行')
	}

};
export default {
	//初始化
	init() {
		if (!window.chrome.webview) return
		window.chrome.webview.addEventListener("message", function(event) {
			// console.log(JSON.stringify(event.data))
			// let res = JSON.parse(event.data);
			let res = event.data;
			res = {
				name: res.Command,
				success: res.Code == '200' ? true : false,
				data: res.Data?JSON.parse(res.Data):{}, //返回的数据
				msg: res.Message,
			}
			console.log('\x1B[41m%s\x1B[0m', '------', `container收到[${res.name}]:`, JSON.stringify(event.data))
			if (res.success) {
				window.xhContainer[res.name].success(res);
			} else {
				window.xhContainer[res.name].fail && window.xhContainer[res.name].fail(res);
				Toast({
					message: res.msg
				});
			}
		});
		console.log('Windows容器[初始化成功]')
	},
	//清除缓存
	clearCache() {
		window.chrome.webview.postMessage('CLEARCACHE');
		console.log('清除缓存')
	},
	//重载
	reLoad() {
		window.chrome.webview.postMessage('RELOAD');
	},
	//退出程序
	closeApp() {
		window.chrome.webview.postMessage('CLOSE');
	},
	//切换默认打印机
	setDefaultPrinter(params) {
		postMessage({
			name: 'SetDefaultPrinter',
			data: {
				"PrinterName": store.state.deviceInfo.reportPrintName,
			},
			success: () => {
				params.success();
			},
			fail: (err) => {
				params.fail(err);
			}
		})
	},
	//刷脸获取用户信息
	getUserInfoByFace(params) {
		postMessage({
			name: 'NationECCode',
			data: {
				"transType": "cn.nhsa.ec.auth",
				...params.data,
			},
			success: (res) => {
				let result = res.data;
				if (result.code == 0) {
					params.success(result.data);
				} else {
					Toast({
						message: result.message,
					});
					params.fail();
				}
			},
			fail: (err) => {
				params.fail(err);
			}
		})
	},
	//获取用户信息解密（Token）
	getUserInfoByFaceCheck(params) {
		postMessage({
			name: 'NationECCode',
			data: {
				"transType": "cn.nhsa.auth.check",
				...params.data,
			},
			success: (res) => {
				let result = res.data;
				if (result.code == 0) {
					params.success({
						...result.data,
						rawData: result,
					});
				} else {
					Toast({
						message: result.message,
					});
					params.fail();
				}
			},
			fail: (err) => {
				params.fail(err);
			}
		})
	},
	//刷脸获取付款码
	getPayCodeByFace(params) {
		postMessage({
			name: 'IotTerminal',
			data: {
				"transType": "iot.get.paycode",
				...params.data,
			},
			success: (res) => {
				let result = res.data;
				if (result.code == 0) {
					params.success(result.data);
				} else {
					Toast({
						message: result.message,
					});
					params.fail({
						msg: result.message
					});
				}
			},
			fail: (err) => {
				params.fail(err);
			}
		})
	},
	//打印pdf
	printPDF(params) {
		postMessage({
			name: 'PdfDocumentPrint',
			data: {
				"PrinterName": store.state.deviceInfo.reportPrintName,
				"UserID": store.state.userInfo.patientId,
				"PrintContent": params.data.pdfUrl,
				"PrintContentType": params.data.type, //http或者ie

			},
			success: () => {
				params.success();
			},
			fail: (err) => {
				params.fail(err);
			}
		})
	},
	//查询报告打印机状态
	queryReportPrint(params) {
		postMessage({
			name: 'PrinterStatus',
			data: {
				"PrinterName": store.state.deviceInfo.reportPrintName,
			},
			success: (res) => {
				params.success(res.data);
			},
			fail: (res) => {
				params.fail(res);
			}
		})
	},
	//社保卡PIN码校验
	iVerifyPIN(params) {
		postMessage({
			name: 'WebSocketProxy',
			data: {
				"ProxyUri": "ws://127.0.0.1:6789",
				"ProxyData": {
					"method": "iVerifyPIN",
					"iType": 1
				}
			},
			success: (res) => {
				params.success(res.data);
			},
			fail: (res) => {
				params.fail(res);
			}
		})
	},

}